﻿using Framework.Utility.Helper;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.Reflection;
using System.Text;

namespace Framework.Utility.Config
{
    public class GlobalConfig
    {
        /// <summary>
        /// All registered service and class instance container. Which are used for dependency injection.
        /// </summary>
        public static IServiceCollection Services { get; set; }
        /// <summary>
        /// Configured service provider.
        /// </summary>
        public static IServiceProvider ServiceProvider { get; set; }

        public static IConfiguration Configuration { get; set; }

        //public static IHostingEnvironment HostingEnvironment { get; set; }

        public static SystemConfig SystemConfig { get; set; } = new SystemConfig();
        public static MailOptions MailOptions { get; set; } = new MailOptions();

        public static JwtSettings JwtSettings { get; set; } = new JwtSettings();

        public static string wwwwroot { get { return Path.Combine(Directory.GetCurrentDirectory(), "wwwroot"); } }
        public static string RootDirectory { get { return Directory.GetCurrentDirectory(); } }
        
       public static string ApplicationName { get; set; }

        public static string EnvironmentName { get; set; } = "";

        public static string ContentRootPath { get; set; }


        public static string HostUrl { get; set; }

        public static string GetVersion()
        {
            Version version = Assembly.GetEntryAssembly()?.GetName().Version??new Version(1,0);
            return version.Major + "." + version.Minor;
        }

        /// <summary>
        /// 程序启动时，记录目录
        /// </summary>
        /// <param name="env"></param>
        public static void LogWhenStart()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("程序启动 ");
            sb.Append("\n ContentRootPath:" + ContentRootPath);
            sb.Append("\n WebRootPath:" + RootDirectory);
            sb.Append("\n IsDevelopment:" + EnvironmentName);
            LogHelper.Warn(sb.ToString());
        }
    }

    public class WebOnLine {
        /// <summary>
        /// 在线用户数量
        /// </summary>
        public static int OnLineCount { get { return OnLineList.Count; } }
        /// <summary>
        /// 在线用户ID
        /// </summary>
        public static List<OnLineInfo> OnLineList { get; set; } = new List<OnLineInfo>() {
            new OnLineInfo{ 
                Id="1000001",
                Name="admin",
                LoginTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm")
            }
        };
    }
    public class OnLineInfo
    { 
        public  string Id { get; set; }
 
        public string Name { get; set; }

        public string LoginTime { get; set; }
    }

}
